package com.chaoxing.ai.speech.Controller;

import com.chaoxing.ai.speech.service.SpeechService;
import com.chaoxing.ai.speech.utils.FFmpegAudioUtil;
import com.chaoxing.ai.speech.utils.SamplingRateTest;
import com.chaoxing.ai.speech.utils.SoundConvert;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/ai/speech")
public class Speech1Controller {
    @Resource
    private SoundConvert soundConvert;
    @Resource
    private SpeechService speechService;
    @Resource
    private FFmpegAudioUtil fFmpegAudioUtil;

    @RequestMapping(value = "/recognition", method = RequestMethod.POST)
    public String recognition(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String templatePath = "F:\\test";       //设置文件上传的路径
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        //获取文件名
        String prefixName = fileName.substring(0, fileName.lastIndexOf("."));
        //获取文件类型
        String contentType = file.getContentType();
        long size = file.getSize();
        System.out.println(suffixName);
        if (!suffixName.equals("wav") && !suffixName.equals("mp3") && !suffixName.equals("m4a") && !suffixName.equals("pcm") && !suffixName.equals("amr")) {
            return "Format Error!";
        } else if (size > 1024 * 1024) {
            return "Size Error!";
        }

        //UUID uuid = UUID.randomUUID();
        File dest0 = new File(templatePath);
        File dest = new File(dest0, File.separator + fileName);
        //文件上传-覆盖
        try {
            // 检测是否存在目录
            if (!dest0.getParentFile().exists()) {
                dest0.getParentFile().mkdirs();
                //检测文件是否存在
            }
            if (!dest.exists()) {
                dest.mkdirs();
            }
            file.transferTo(dest);
            String newfilePath = null;
            //m4a格式音频转换wav格式
            if (suffixName.equals("m4a")) {
                suffixName = "wav";
                String m4afilePath = templatePath + "\\" + fileName;
                fileName = prefixName + "." + suffixName;
                newfilePath = templatePath + "\\" + fileName;
                System.out.println(m4afilePath);
                System.out.println(newfilePath);
                fFmpegAudioUtil.ffM4AtoWAV(m4afilePath, newfilePath);
                dest.delete();
                dest = new File(dest0, File.separator + fileName);      //指向m4a转wav音频文件
            }
            //amr格式音频转mp3格式
            if (suffixName.equals("amr")) {
                suffixName = "mp3";
                String amrfilePath = templatePath + "\\" + fileName;
                System.out.println(amrfilePath);
                fileName = prefixName + "." + suffixName;
                newfilePath = templatePath + "\\" + fileName;
                System.out.println(newfilePath);
                fFmpegAudioUtil.ffAmrToMp3(amrfilePath, newfilePath);
//                SoundConvert.amrToMp3(amrfilePath,newfilePath);
                dest.delete();                                             //删除amr音频文件
                dest = new File(dest0, File.separator + fileName);      //指向amr转MP3音频文件
            }
            //mp3格式音频转换为pcm格式
            if (suffixName.equals("mp3")) {
                suffixName = "pcm";
                String mp3filePath = templatePath + "\\" + fileName;
                fileName = prefixName + "." + suffixName;
                newfilePath = templatePath + "\\" + fileName;
                //SoundConvert.Mp3ToPcm(mp3filePath,newfilePath);
                fFmpegAudioUtil.ffMP3toPCM(mp3filePath, newfilePath);
                dest.delete();                                              //删除MP3音频文件
                dest = new File(dest0, File.separator + fileName);      //指向MP3转PCM音频文件
            }
            //pcm格式音频转换为wav格式
            Integer integer = null;
            if (suffixName.equals("pcm")) {
                suffixName = "wav";
                String filePath = templatePath + "\\" + fileName;         //需要进行转化的文件
                fileName = prefixName + "." + suffixName;
                newfilePath = templatePath + "\\" + fileName;                   //存储的新的文件名
                integer = SamplingRateTest.frequency(dest.getPath());         //获取采样频率
                System.out.println(integer);
                if (integer >= 14000) {
                    fFmpegAudioUtil.ffPCMtoWAV8K(filePath, newfilePath);
                } else {
                    fFmpegAudioUtil.ffPCMtoWAV16K(filePath, newfilePath);
                }
                dest.delete();                                         //删除PCM音频文件
                dest = new File(dest0, File.separator + fileName);  //指向PCM转换WAV的音频文件
            }
            integer = SamplingRateTest.frequency(dest.getPath());         //获取采样频率
            System.out.println(integer + "  嘿嘿");
            String language = null;
            String filePath = templatePath + "\\" + fileName;
            File source = new File(filePath);
            Encoder encoder = new Encoder();
            MultimediaInfo m = encoder.getInfo(source);
            Double isTime30s = Double.parseDouble(m.getDuration() / 1000 + "");
            if (isTime30s >= 30.0) {
                return "Time Error!";
            }
            if (integer > 8000) {
                language = speechService.speechRecognition(templatePath, fileName, suffixName);
            } else {
                language = speechService.speechRecognition8(templatePath, fileName, suffixName);
            }
            dest.delete();                          //删除WAV音频文件
            return language;
        } catch (Exception e) {
            e.printStackTrace();
            return "解析发生错误";
        }
    }
}
